package com.example.teacherservice.service;

import com.example.teacherservice.dto.ClassInfoDTO;
import com.example.teacherservice.dto.CourseInfoDTO;
import com.example.teacherservice.entity.Teacher;
import com.example.teacherservice.repository.TeaClassRepository;
import com.example.teacherservice.repository.TeacherRepository;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class TeacherService {
    private final TeacherRepository teacherRepository;
    private final TeaClassRepository teaClassRepository;

    public TeacherService(TeacherRepository teacherRepository, TeaClassRepository teaClassRepository) {
        this.teacherRepository = teacherRepository;
        this.teaClassRepository = teaClassRepository;
    }

    public Teacher getTeacherById(Long id) {
        return teacherRepository.findById(id).orElse(null);
    }

    public Teacher getTeacherByUserId(Long userId) {
        return teacherRepository.findByUserId(userId).orElse(null);
    }

    public Long getUserIdByTeacherId(Long id) {
        return teacherRepository.findById(id).map(Teacher::getUserId).orElse(null);
    }

    public Teacher updateTeacher(Teacher teacher) {
        return teacherRepository.save(teacher);
    }


    // 获取教师教授的班级信息
    public List<ClassInfoDTO> getTeacherClasses(Long teacherId) {
        List<Map<String, Object>> result = teacherRepository.findTeacherClasses(teacherId);
        return result.stream().map(this::convertToClassInfoDTO).collect(Collectors.toList());
    }

    // 获取教师教授的课程信息
    public List<CourseInfoDTO> getTeacherCourses(Long teacherId) {
        List<Map<String, Object>> result = teacherRepository.findTeacherCourses(teacherId);
        return result.stream().map(this::convertToCourseInfoDTO).collect(Collectors.toList());
    }

    private ClassInfoDTO convertToClassInfoDTO(Map<String, Object> map) {
        ClassInfoDTO dto = new ClassInfoDTO();
        dto.setId(((Number) map.get("id")).longValue());
        dto.setClassName((String) map.get("className"));
        dto.setStudentCount(((Number) map.get("studentCount")).intValue());
        dto.setCreateTime((Timestamp) map.get("create_time"));
        return dto;
    }

    private CourseInfoDTO convertToCourseInfoDTO(Map<String, Object> map) {
        CourseInfoDTO dto = new CourseInfoDTO();
        dto.setId(((Number) map.get("id")).longValue());
        dto.setCourseName((String) map.get("courseName"));
        dto.setChapterCount(((Number) map.get("chapterCount")).intValue());
        dto.setStudentCount(((Number) map.get("studentCount")).intValue());
        dto.setCreateTime((Timestamp) map.get("create_time"));
        return dto;
    }

    // TeacherService.java
    public boolean checkTeacherClassRelation(Long teacherId, Long classId) {
        // 方法1：使用JPA查询方法（推荐）
        return teaClassRepository.existsByTeaIdAndClassId(teacherId, classId);

        // 方法2：使用自定义SQL查询（备选）
        // return teacherRepository.existsTeacherClassRelation(teacherId, classId) > 0;
    }
}